# -*- coding: utf-8 -*-
'''
Resource
   File
   Container
     Project
     Dir
'''
from path import Path
import file_system

class Repository(object):
    
    def __init__(self):
        self._dataTree=_DataTree()
        
    def getProject(self,path):
        return Project(path,self)
    
class Resource(object):
    def __init__(self,path,repository):
        self._path=path
        self._repository=repository
    
    def getPath(self):
        return self._path
    
    def exists(self):
        info = self.getResourceInfo()
        return True if (info is not None and info.exists) else False
    
    def getStore(self):
        return file_system.getFileStore(self.getPath())
    
    def getResourceInfo(self):
        return self._repository._dataTree.getInfo(self._path)
    
class File(Resource):
    pass

class Container(Resource):
    def getDir(self,name):
        return Dir(self._path + name,self._repository)

class Project(Container):
    def open(self):
        pass
    
    def isOpen(self):
        info = self.getResourceInfo()
        return True if (info is not None and info.isOpen) else False
    
    def create(self,name):
        info=ResourceInfo()
        info.name=name
        info.path=self._path
        info.exists=True
        self.getStore().makedir()
        self._repository._dataTree.addProject(self._path,info)

class Dir(Container):
    def create(self):
        info=ResourceInfo()
        info.name=self._path.getLastSegment()
        info.path=self._path
        info.exists=True
        self.getStore().makedir()
        self._repository._dataTree.addNode(self._path,info)


class ResourceInfo(object):
    def __init__(self):
        self.name = ""
        
        self.size=-1
        self.modifyTime=-1
        self.type=None
        self.exists=False
        
        #project attribute
        self.path = None
        self.isOpen=False

class _DataTreeNode(object):
    def __init__(self,tree,parent,key,data):
        self._tree=tree
        self._parent=parent
        self._key=key
        self._data=data
        self._children={}
        
    def __contains__(self,key):
        return key in self._children
    
    def addChild(self,nodeKey,nodeData):
        self._children[nodeKey]=_DataTreeNode(self._tree,self,nodeKey,nodeData)
        
    def isRoot(self):
        return self._parent==None
    
    def isProject(self):
        return self.getParent() == self._tree._root

    def getChild(self,key):
        return self._children[key]

class _DataTree(object):
    
    def __init__(self):
        self._root=_DataTreeNode(self,None,"root","root data")

    def include(self,nodePath):
        '''exists(Path nodePath)-> bool'''
        return True if __findNode(nodePath) is not None else False
    
    def getInfo(self,nodePath):
        '''getInfo(Path nodePath)->ResourceInfo'''
        childNode=self.__findNode(nodePath)
        return childNode._data if childNode is not None else None
    
    def __findNode(self,nodePath):
        for projectPath,projectNode in self._root._children.items():
            if projectPath.isPrefixOf(nodePath):
                relativePath = nodePath - projectPath
                result=projectNode
                for segment in relativePath.getSegments():
                    if segment not in result:
                        return None
                    result = result.getChild(segment)
                return result
            
        return None
    
    def addProject(self,nodePath,nodeData):
        '''addProject(Path nodePath,object nodeData)-> bool'''
        self._root.addChild(nodePath,nodeData)
    
    def addNode(self,nodePath,nodeData):
        self.__findNode(nodePath.getParent()).addChild(nodePath.getLastSegment(),nodeData)
    